package cn.zc.auth.service.controller;


import cn.tools.core.base.R;
import cn.tools.jwt.client.service.JwtCreateTokenService;
import cn.zc.auth.feign.entity.Dto.StudentUserDto;

import cn.zc.auth.feign.entity.VO.StudentUserVO;
import cn.zc.auth.service.service.StudentUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
//import net.oschina.j2cache.CacheChannel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.security.NoSuchAlgorithmException;
import java.util.Map;

/**
 * <p>
 * 学生用户表 前端控制器
 * </p>
 *
 * @author keno
 * @since 2022-02-12
 */
@Api(tags = "学生登录注册接口")
@RestController
@RequestMapping("/auth/studentUser")
public class StudentUserController {

    @Resource
    private StudentUserService userService;

    @Resource
    private JwtCreateTokenService jwtCreateTokenService;

//    @Resource
//    private CacheChannel cacheChannel;

    private String key = "test";
    private String addr = "rx";

    /**
     * 学生登录接口
     * @param userName
     * @param passWord
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "学生账号密码登录接口")
    @GetMapping("/studentLogin")
    private R<Object> studentLogin(@NotNull String userName,@NotNull String passWord) throws Exception {
       Map<String,Object> userInfo = userService.studentLogin(userName,passWord);
       String jwt = jwtCreateTokenService.createJwt(userInfo);
//       cacheChannel.set(addr,key,jwt);
       return R.success(jwt);
    }


    /**
     * 用户注册接口
     * @param studentUserDto
     * @return
     */
    @ApiOperation(value = "学生注册账户接口")
    @GetMapping("/createStudent")
    private R createStudent(StudentUserDto studentUserDto){
        userService.createStudent(studentUserDto);
        return R.success();
    }

    @GetMapping("/createJwt")
    private String createJwt() throws NoSuchAlgorithmException {
        jwtCreateTokenService.createKeyFile();
        return "创建成功";
    }
}

